home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / lisp / wgdb-42.lha / wgdb-4.2 / gdb / mips-pinsn.c < prev    next >
C/C++ Source or Header  |  1992-09-11  |  4KB  |  150 lines

  1. /* Print mips instructions for GDB, the GNU debugger.
  2.    Copyright (C) 1989 Free Software Foundation, Inc.
  3.    Contributed by Nobuyuki Hikichi(hikichi@sra.co.jp)
  4.  
  5. This file is part of GDB.
  6.  
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11.  
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21. #include <stdio.h>
  22.  
  23. #include "defs.h"
  24. #include "param.h"
  25. #include "symtab.h"
  26. #include "mips-opcode.h"
  27.  
  28. /* Mips instructions are never longer than this many bytes.  */
  29. #define MAXLEN 4
  30.  
  31. /* Number of elements in the opcode table.  */
  32. #define NOPCODES (sizeof mips_opcodes / sizeof mips_opcodes[0])
  33.  
  34. #define MKLONG(p)  *(unsigned long*)p
  35.  
  36. extern char *reg_names[];
  37.  
  38.  
  39. /* subroutine */
  40. static unsigned char *
  41. print_insn_arg (d, l, stream, pc)
  42.      char *d;
  43.      register unsigned long int *l;
  44.      FILE *stream;
  45.      CORE_ADDR pc;
  46. {
  47.   switch (*d)
  48.     {
  49.     case ',':
  50.     case '(':
  51.     case ')':
  52.       fputc (*d, stream);
  53.       break;
  54.  
  55.     case 's':
  56.       fprintf (stream, "$%s", reg_names[((struct op_i_fmt *) l)->rs]);
  57.       break;
  58.  
  59.     case 't':
  60.       fprintf (stream, "$%s", reg_names[((struct op_i_fmt *) l)->rt]);
  61.       break;
  62.  
  63.     case 'i':
  64.       fprintf (stream, "%d", ((struct op_i_fmt *) l)->immediate);
  65.       break;
  66.  
  67.     case 'j': /* same as i, but sign-extended */
  68.       fprintf (stream, "%d", ((struct op_b_fmt *) l)->delta);
  69.       break;
  70.  
  71.     case 'a':
  72.       print_address ((pc & 0xF0000000) | (((struct op_j_fmt *)l)->target << 2),
  73.              stream);
  74.       break;
  75.  
  76.     case 'b':
  77.       print_address ((((struct op_b_fmt *) l)->delta << 2) + pc + 4, stream);
  78.       break;
  79.  
  80.     case 'd':
  81.       fprintf (stream, "$%s", reg_names[((struct op_r_fmt *) l)->rd]);
  82.       break;
  83.  
  84.     case 'h':
  85.       fprintf (stream, "0x%x", ((struct op_r_fmt *) l)->shamt);
  86.       break;
  87.  
  88.     case 'S':
  89.       fprintf (stream, "$f%d", ((struct fop_r_fmt *) l)->fs);
  90.       break;
  91.  
  92.     case 'T':
  93.       fprintf (stream, "$f%d", ((struct fop_r_fmt *) l)->ft);
  94.       break;
  95.  
  96.     case 'D':
  97.       fprintf (stream, "$f%d", ((struct fop_r_fmt *) l)->fd);
  98.       break;
  99.  
  100.     default:
  101.       fprintf (stream, "# internal error, undefined modifier(%c)", *d);
  102.       break;
  103.     }
  104. }
  105.  
  106. /* Print the mips instruction at address MEMADDR in debugged memory,
  107.    on STREAM.  Returns length of the instruction, in bytes, which
  108.    is always 4.  */
  109.  
  110. int
  111. print_insn (memaddr, stream)
  112.      CORE_ADDR memaddr;
  113.      FILE *stream;
  114. {
  115.   unsigned char buffer[MAXLEN];
  116.   register int i;
  117.   register char *d;
  118.   unsigned long int l;
  119.  
  120.   read_memory (memaddr, buffer, MAXLEN);
  121.  
  122.   for (i = 0; i < NOPCODES; i++)
  123.     {
  124.       register unsigned int opcode = mips_opcodes[i].opcode;
  125.       register unsigned int match = mips_opcodes[i].match;
  126.       if ((*(unsigned int*)buffer & match) == opcode)
  127.     break;
  128.     }
  129.  
  130.   l = MKLONG (buffer);
  131.   /* Handle undefined instructions.  */
  132.   if (i == NOPCODES)
  133.     {
  134.       fprintf (stream, "0x%x",l);
  135.       return 4;
  136.     }
  137.  
  138.   fprintf (stream, "%s", mips_opcodes[i].name);
  139.  
  140.   if (!(d = mips_opcodes[i].args))
  141.     return 4;
  142.  
  143.   fputc (' ', stream);
  144.  
  145.   while (*d)
  146.     print_insn_arg (d++, &l, stream, memaddr);
  147.  
  148.   return 4;
  149. }
  150.